package com.xxxx.server.controller;


import com.xxxx.server.pojo.Employee;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.pojo.Salary;
import com.xxxx.server.service.IEmployeeService;
import com.xxxx.server.service.ISalaryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2020-09-07
 */
@RestController
@RequestMapping("/salary")
public class SalaryController {

    @Autowired
    private ISalaryService salaryService;
    @Autowired
    private IEmployeeService employeeService;

    @ApiOperation("获取所有工资账套信息")
    @GetMapping("/sob")
    public List<Salary> salarySob() {
        final List<Salary> list = salaryService.list();
        return list;
    }

    @ApiOperation("添加工资账套信息")
    @PostMapping("/sob")
    public RespBean doAddSob(@RequestBody Salary salary) {
        salary.setCreateDate(LocalDateTime.now());
        final boolean b = salaryService.saveOrUpdate(salary);
        return getRespBean(b, "添加工资账套信息");
    }

    @ApiOperation("修改工资账套信息")
    @PutMapping("/sob")
    public RespBean doUpdateSob(@RequestBody Salary salary){
        final boolean b = salaryService.saveOrUpdate(salary);
        return getRespBean(b, "修改工资账套信息");
    }

    @ApiOperation("删除工资账套信息")
    @DeleteMapping("/sob/{id}")
    public RespBean doDelSob(@PathVariable("id") Integer id){
        final boolean b = salaryService.removeById(id);
        return getRespBean(b, "删除工资账套信息");
    }

    @ApiOperation("获取所有员工信息带部门")
    @GetMapping("/sobcfg")
    public Map<String,Object> showEmpWithDept(Integer currentPage, Integer size){
        final HashMap<String, Object> map = new HashMap<>();
        final List<Employee> list = employeeService.getAllEmpInfoWithDeptName(currentPage,size);
        final int count = employeeService.count();
        map.put("data",list);
        map.put("total",count);
        return map;
    }

    @ApiOperation("获取薪水的信息")
    @GetMapping("/sobcfg/salaries")
    public List<Salary> showEmpWithSalary(){
        return salaryService.list();
    }

    @ApiOperation("修改员工工资账套信息")
    @PutMapping("/sobcfg")
    public RespBean doUpdateSobCfg(Integer eid,Integer sid){
        /*
        * eid => 员工id
        * sid => 薪水id
        * */
        final Employee employee = new Employee();
        employee.setId(eid);
        employee.setSalaryId(sid);
        final boolean b = employeeService.updateById(employee);
        return getRespBean(b, "修改员工工资账套信息");
    }

    private RespBean getRespBean(boolean b, String operation) {
        if (b) {
            return RespBean.success(operation+"成功");
        } else {
            return RespBean.success(operation+"失败");
        }
    }

}
